package com.taomus.mytools.other.utils;

public class EscapeUtils {
	public static String escapeLuceneQueryChars(String s) {
	    StringBuilder sb = new StringBuilder();
	    for (int i = 0; i < s.length(); i++) {
	      char c = s.charAt(i);
	      // These characters are part of the query syntax and must be escaped
	      if (c == '\\' || c == '+' || c == '-' || c == '!'  || c == '(' || c == ')' || c == ':'
	        || c == '^' || c == '[' || c == ']' || c == '\"' || c == '{' || c == '}' || c == '~'
	        || c == '*' || c == '?' || c == '|' || c == '&'  || c == ';' || c == '/'
	        || Character.isWhitespace(c)) {
	        sb.append('\\');
	      }
	      sb.append(c);
	    }
	    return sb.toString();
}
 public static String escapeRegxQueryChars(String s) {
	 StringBuilder sb = new StringBuilder();
	    for (int i = 0; i < s.length(); i++) {
	      char c = s.charAt(i);
	      // These characters are part of the query syntax and must be escaped
	      if (c == '$' || c == '(' || c == ')' || c=='*' || c == '+' || c == '-'
	         || c == '.' || c == '[' || c == ']' || c == '?' || c == '\'' 
	    	 || c == '\\' || c == '^' || c == '{' || c == '}' || c == '|' || c == '/' || c == '\"'
	    	) {
	        sb.append('\\');
	      }
	      sb.append(c);
	    }
	    return sb.toString(); 
 }
}
